<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务ID" prop="taskId">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入任务ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易量" prop="tradingVolume">
        <el-input
          v-model="queryParams.tradingVolume"
          placeholder="请输入交易量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="买家旺旺" prop="buyerNick">
        <el-input
          v-model="queryParams.buyerNick"
          placeholder="请输入买家旺旺"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易类型" prop="tradingType">
        <el-select v-model="queryParams.tradingType" placeholder="请选择交易类型" clearable size="small">
          <el-option
            v-for="dict in tradingTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable size="small">
          <el-option
            v-for="dict in orderTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="返款类型" prop="rebatesType">
        <el-select v-model="queryParams.rebatesType" placeholder="请选择返款类型" clearable size="small">
          <el-option
            v-for="dict in rebatesTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户类型" prop="buyerType">
        <el-select v-model="queryParams.buyerType" placeholder="请选择用户类型" clearable size="small">
          <el-option
            v-for="dict in buyerTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="返款渠道" prop="rebatesChannel">
        <el-select v-model="queryParams.rebatesChannel" placeholder="请选择返款渠道" clearable size="small">
          <el-option
            v-for="dict in rebatesChannelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="返款金额" prop="rebatesAmount">
        <el-input
          v-model="queryParams.rebatesAmount"
          placeholder="请输入返款金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="责任人 ID" prop="responsibleUserId">
        <el-input
          v-model="queryParams.responsibleUserId"
          placeholder="请输入责任人 ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:detail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:detail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:detail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:detail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务明细ID" align="center" prop="taskDetailId" />
      <el-table-column label="任务ID" align="center" prop="taskId" />
      <el-table-column label="交易量" align="center" prop="tradingVolume" />
      <el-table-column label="买家旺旺" align="center" prop="buyerNick" />
      <el-table-column label="交易类型" align="center" prop="tradingType" :formatter="tradingTypeFormat" />
      <el-table-column label="订单类型" align="center" prop="orderType" :formatter="orderTypeFormat" />
      <el-table-column label="返款类型" align="center" prop="rebatesType" :formatter="rebatesTypeFormat" />
      <el-table-column label="用户类型" align="center" prop="buyerType" :formatter="buyerTypeFormat" />
      <el-table-column label="返款渠道" align="center" prop="rebatesChannel" :formatter="rebatesChannelFormat" />
      <el-table-column label="返款渠道明细" align="center" prop="rebatesWay" />
      <el-table-column label="返款金额" align="center" prop="rebatesAmount" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="责任人 ID" align="center" prop="responsibleUserId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:detail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务完成明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务ID" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入任务ID" />
        </el-form-item>
        <el-form-item label="交易量" prop="tradingVolume">
          <el-input v-model="form.tradingVolume" placeholder="请输入交易量" />
        </el-form-item>
        <el-form-item label="买家旺旺" prop="buyerNick">
          <el-input v-model="form.buyerNick" placeholder="请输入买家旺旺" />
        </el-form-item>
        <el-form-item label="交易类型" prop="tradingType">
          <el-select v-model="form.tradingType" placeholder="请选择交易类型">
            <el-option
              v-for="dict in tradingTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择订单类型">
            <el-option
              v-for="dict in orderTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="返款类型" prop="rebatesType">
          <el-select v-model="form.rebatesType" placeholder="请选择返款类型">
            <el-option
              v-for="dict in rebatesTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户类型" prop="buyerType">
          <el-select v-model="form.buyerType" placeholder="请选择用户类型">
            <el-option
              v-for="dict in buyerTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="返款渠道" prop="rebatesChannel">
          <el-select v-model="form.rebatesChannel" placeholder="请选择返款渠道">
            <el-option
              v-for="dict in rebatesChannelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="返款渠道明细" prop="rebatesWay">
          <el-input v-model="form.rebatesWay" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="返款金额" prop="rebatesAmount">
          <el-input v-model="form.rebatesAmount" placeholder="请输入返款金额" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="责任人 ID" prop="responsibleUserId">
          <el-input v-model="form.responsibleUserId" placeholder="请输入责任人 ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetail, getDetail, delDetail, addDetail, updateDetail, exportDetail } from "@/api/system/detail";

export default {
  name: "Detail",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务完成明细表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 交易类型字典
      tradingTypeOptions: [],
      // 订单类型字典
      orderTypeOptions: [],
      // 返款类型字典
      rebatesTypeOptions: [],
      // 用户类型字典
      buyerTypeOptions: [],
      // 返款渠道字典
      rebatesChannelOptions: [],
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        tradingVolume: null,
        buyerNick: null,
        tradingType: null,
        orderType: null,
        rebatesType: null,
        buyerType: null,
        rebatesChannel: null,
        rebatesWay: null,
        rebatesAmount: null,
        status: null,
        responsibleUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskId: [
          { required: true, message: "任务ID不能为空", trigger: "blur" }
        ],
        buyerNick: [
          { required: true, message: "买家旺旺不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("trading_type").then(response => {
      this.tradingTypeOptions = response.data;
    });
    this.getDicts("order_type").then(response => {
      this.orderTypeOptions = response.data;
    });
    this.getDicts("rebates_type").then(response => {
      this.rebatesTypeOptions = response.data;
    });
    this.getDicts("user_type").then(response => {
      this.buyerTypeOptions = response.data;
    });
    this.getDicts("channel_type").then(response => {
      this.rebatesChannelOptions = response.data;
    });
    this.getDicts("risk_detail_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询任务完成明细列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 交易类型字典翻译
    tradingTypeFormat(row, column) {
      return this.selectDictLabel(this.tradingTypeOptions, row.tradingType);
    },
    // 订单类型字典翻译
    orderTypeFormat(row, column) {
      return this.selectDictLabel(this.orderTypeOptions, row.orderType);
    },
    // 返款类型字典翻译
    rebatesTypeFormat(row, column) {
      return this.selectDictLabel(this.rebatesTypeOptions, row.rebatesType);
    },
    // 用户类型字典翻译
    buyerTypeFormat(row, column) {
      return this.selectDictLabel(this.buyerTypeOptions, row.buyerType);
    },
    // 返款渠道字典翻译
    rebatesChannelFormat(row, column) {
      return this.selectDictLabel(this.rebatesChannelOptions, row.rebatesChannel);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskDetailId: null,
        taskId: null,
        tradingVolume: null,
        buyerNick: null,
        tradingType: null,
        orderType: null,
        rebatesType: null,
        buyerType: null,
        rebatesChannel: null,
        rebatesWay: null,
        rebatesAmount: null,
        status: "0",
        responsibleUserId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskDetailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务完成明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const taskDetailId = row.taskDetailId || this.ids
      getDetail(taskDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务完成明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.taskDetailId != null) {
            updateDetail(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetail(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskDetailIds = row.taskDetailId || this.ids;
      this.$confirm('是否确认删除任务完成明细编号为"' + taskDetailIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDetail(taskDetailIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有任务完成明细数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDetail(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>